﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Workflow.Flow;
using Jzy.Core.Common.Web;

using Jzy.Core.Common;
using Jzy.Workflow.Form;
using Jzy.Workflow.Task;
using Frame.Lib.Web;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Jzy.Web.WorkFlow.Fore
{
    public partial class DealtList : PageList
    {
        private FlowTaskBO _task = new FlowTaskBO();
        private PagerSortSetting _setting;

        protected DealtListTab TabIndex
        {
            get
            {
                if (string.IsNullOrEmpty(Request["TabIndex"]))
                {
                    return DealtListTab.我起草的;
                }
                else
                {
                    return (DealtListTab)int.Parse(Request["TabIndex"]);
                }
            }
        }

        //表单状态的筛选，默变为全部
        private int FormState
        {
            get
            {
                if (ViewState["FormState"] == null)
                {
                    ViewState["FormState"] = -1;
                }
                return (int)ViewState["FormState"];
            }
            set
            {
                ViewState["FormState"] = value;
            }
        }

        protected string GetClassName(object tabIndex)
        {
            if ((int)tabIndex == (int)this.TabIndex)
            {
                return "on";
            }
            else
            {
                return "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Pager1.InitPager(new BindViewHandler(BindView));
            CssFileManager.AddCssFile(CssFile.tab1);
            if (!IsPostBack)
            {
                BindTab();
                BindView();
            }
        }

        private void BindView()
        {
            if (FormState == -1)
            {
                pnlState.Visible = false;
            }
            else
            {
                pnlState.Visible = true;
            }

            IEnumerable<FlowTaskInfo> list = _task.GetDealtList(AppSession.UserID).OrderByDescending(t => t.FinishTime);
            if (FormState != -1)
            {
                list = list.Where(t => t.FormInfo.State == FormState);
            }

            if (TabIndex == DealtListTab.我起草的)
            {
                lv0.EnableViewState = true;//否则事件不起作用

                holder0.Visible = true;
                holder1.Visible = false;
                lv0.DataSource = _setting.GetList<FlowTaskInfo>(list.Where(t => t.FormInfo.CreatorID == AppSession.UserID));
                lv0.DataBind();
            }
            else
            {
                holder0.Visible = false;
                holder1.Visible = true;
                lv1.DataSource = _setting.GetList<FlowTaskInfo>(list.Where(t => t.FormInfo.CreatorID != AppSession.UserID));
                lv1.DataBind();
            }
            

        }
        protected FlowFormInfo GetForm(object form)
        {
            return (FlowFormInfo)form;
        }

        private void BindTab()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(DealtListTab));
            rpt.DataSource = list;
            rpt.DataBind();

            list = ConvertUtil.ConvertEnum(typeof(FormState));
            EnumInfo info = new EnumInfo(-1,"全部");
            list.Insert(0, info);
            rptState.DataSource = list;
            rptState.DataBind();

        }

        protected void rptState_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            FormState = Convert.ToInt16(e.CommandArgument);
            if (FormState != -1)
            {
                lblState.Text = ((Jzy.Workflow.Form.FormState)FormState).ToString();
            }
            BindView();
        }

        protected void lv0_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            string[] tmp = e.CommandArgument.ToString().Split(',');
            int id = int.Parse(tmp[0]);
            int state = int.Parse(tmp[1]);
            FlowFormBO form = new FlowFormBO();
            if (state == (int)Jzy.Workflow.Form.FormState.流转中)
            {
                form.SetState(id, Jzy.Workflow.Form.FormState.暂停);
            }
            else
            {
                form.SetState(id, Jzy.Workflow.Form.FormState.流转中);
            }
            BindView();
        }

        protected void lv0_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            FlowTaskInfo info = (FlowTaskInfo)((ListViewDataItem)e.Item).DataItem;
            LinkButton btnOperate = (LinkButton)e.Item.FindControl("btnOperate");
            btnOperate.CommandArgument = info.FormInfo.ID + "," + info.FormInfo.State.ToString();
            if (info.FormInfo.State == (int)Jzy.Workflow.Form.FormState.流转中)
            {
                btnOperate.Text = "暂停办理";
                btnOperate.OnClientClick = "return confirm('确认暂停该表单')";

            }
            else if (info.FormInfo.State == (int)Jzy.Workflow.Form.FormState.暂停)
            {
                btnOperate.Text = "恢复办理";
                btnOperate.OnClientClick = "return confirm('确认恢复该表单')";
            }
            else
            {
                btnOperate.Visible = false;
            }
        }

    }
}
